//
//  firstPageViewController.m
//  Wedding fantastic
//
//  Created by dllo on 15/6/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "firstPageViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "NetworkTool.h"
#import "recommendCell.h"
#import "searchViewController.h"
#import "discountModel.h"
#import "labelRecommendModel.h"
#import "recommendWorksModel.h"
#import "RecommendWorksCell.h"
#import "showImageModel.h"
#import "showImageDetailViewController.h"
#import "MBProgressHUD.h"
#import "recommendWorksDetailViewController.h"
#import "discountDetailViewController.h"
#import "discountDetailModel.h"
#import "discountDetailCell.h"
#import "CategoriesViewController.h"
//#import "MJRefresh.h"
#import "wholeLabelViewController.h"

#define kHeight [UIScreen mainScreen].bounds.size.height
#define kWidth [UIScreen mainScreen].bounds.size.width

@interface firstPageViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIView *headerView;
@property(nonatomic, retain)UIScrollView *scrollView;
// 超值折扣按钮
@property(nonatomic, retain)UIButton *discountButton;
// 超值折扣集合视图
@property(nonatomic, retain)UICollectionView *discountCollectionView;
// 轮播图数组
@property(nonatomic, retain)NSMutableArray *picArr;
// 展示图数组
@property(nonatomic, retain)NSMutableArray *showPicArr;
// 超值折扣信息数组
@property(nonatomic, retain)NSMutableArray *discountArr;
// 超值折扣详细页面信息数组
@property(nonatomic, retain)NSMutableArray *discountDetailArr;
// 标签推荐信息数组
@property(nonatomic, retain)NSMutableArray *labelRecommendArr;
// 标签推荐按钮
@property(nonatomic, retain)UIButton *labelRecommendButton;
// 标签推荐集合视图
@property(nonatomic, retain)UICollectionView *labelRecommend;
// 推荐作品信息数组
@property(nonatomic, retain)NSMutableArray *recommendWorksArr;
// 推荐作品详细页面数组
@property(nonatomic, retain)NSMutableArray *recommendWorksDetailArr;
// 展示图右按钮
@property(nonatomic, retain)UIButton *rightShowImage;
// 展示图中间按钮
@property(nonatomic, retain)UIButton *middleShowImage;
// 展示图左按钮
@property(nonatomic, retain)UIButton *leftShowImage;

@end

@implementation firstPageViewController
- (void)dealloc {
    [_tableView release];
    [_headerView release];
    [_scrollView release];
    [_discountButton release];
    [_discountCollectionView release];
    [_picArr release];
    [_showPicArr release];
    [_labelRecommend release];
    [_discountArr release];
    [_labelRecommendArr release];
    [_labelRecommendButton release];
    [_recommendWorksArr release];
    [_recommendWorksDetailArr release];
    [_leftShowImage release];
    [_middleShowImage release];
    [_rightShowImage release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.picArr = [NSMutableArray array];
        self.showPicArr = [NSMutableArray array];
        self.discountArr = [NSMutableArray array];
        self.labelRecommendArr = [NSMutableArray array];
        self.recommendWorksArr = [NSMutableArray array];
        self.recommendWorksDetailArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //
    self.view.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    self.view.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    self.navigationItem.title = @"首页";
    self.navigationController.navigationBar.translucent = NO;
    // 设置标题颜色
    self.navigationController.navigationBar.tintColor = [UIColor brownColor];
    // 字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor brownColor],NSForegroundColorAttributeName,nil]];
    
    
    

    // tableview
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49 * kHeight / 667) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:@selector(search:)];
    
    // headerview
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, (200 + 100 + 150 + 20 + 280 + 20 + 50 + 50 + 30 + 50 + 50) * kHeight / 667)];
    self.headerView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    //设置table的header
    self.tableView.tableHeaderView = self.headerView;
    [_headerView release];
    
    
    // 回到顶部button
    UIButton *topButton = [UIButton buttonWithType:UIButtonTypeCustom];
    topButton.frame = CGRectMake(300 * kWidth / 375, 450 * kHeight / 667, 35 * kWidth / 375, 38 * kHeight / 667);
    [topButton setBackgroundImage:[UIImage imageNamed:@"backTop"] forState:UIControlStateNormal];
    topButton.alpha = 0.6;
    [self.view addSubview:topButton];
    [topButton addTarget:self action:@selector(topAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    // 解析数据(轮播图,展示图)
    [self createData];
    // 解析超值折扣的数据
    [self createDiscountData];
    // 解析数据(标签推荐集合视图)
    [self createLabelRecommend];
    // 解析推荐作品信息
    [self createRecommendWorks];

    
    // 创建视图
    [self createView];

    self.page = 1;
    //网络请求
    [self addHeader];
    [self addFooter];
}
//
- (void)addHeader
{
    __block firstPageViewController *detailVC = self;
    
    // 添加下拉刷新头部控件
    [self.tableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        detailVC.page = 1;
        detailVC.Loading = NO;//标记为下拉操作
        [self reCreateData];//重新请求数据
        
    }];
#pragma mark 自动刷新(一进入程序就下拉刷新)
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    __block firstPageViewController *detailVC = self;
    // 添加上拉刷新尾部控件
    [self.tableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        detailVC.Loading = YES;//标记为上拉操作
        [detailVC reCreateData];//请求更多数据
        
    }];
}
// 加载更多推荐作品
-(void)reCreateData

{
     NSString *urlStr = [NSString stringWithFormat:@"http://www.hunliji.com/v2/api/app/works?commodity_type=1&per_page=20&first_query_time=2015-06-26T01:42:51Z&page=%ld", (long)++self.page];
   [NetworkTool get:urlStr body:nil httpResult:JSON success:^(id result) {
       // 如果成功返回数据, 接收数据
       NSDictionary *dic = result;
       NSArray *worksArr = dic[@"works"];
       for (NSDictionary *tempDic in worksArr) {
           recommendWorksModel *model = [[recommendWorksModel alloc] init];
           [model setValuesForKeysWithDictionary:tempDic];
           [self.recommendWorksArr addObject:model];
           
           
       }
       [self.tableView headerEndRefreshing];
       [self.tableView footerEndRefreshing];
       [self.tableView reloadData];
   } failure:^(NSError *error) {
       
   }];
    
}


// 回到顶部
- (void)topAction:(UIButton *)button {
    [self.tableView setContentOffset:CGPointZero animated:YES];
}




// 查询按钮的触发方法
- (void)search:(UIButton *)button {
    searchViewController *searchVC = [[searchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    
    [searchVC release];
}
// 指定table的分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置分区的标题名
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"推荐作品";
}
// tableview协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.recommendWorksArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    RecommendWorksCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[RecommendWorksCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    recommendWorksModel *model = self.recommendWorksArr[indexPath.row];
    cell.titleLabel.text = model.title;
    cell.merchantLabel.text = model.merchant[@"name"];
    cell.like_countLabel.text = [model.collectors_count stringValue];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_path]];
    return cell;
}
// 被选中表的cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    recommendWorksDetailViewController *recommendWorkDVC = [[recommendWorksDetailViewController alloc] init];
    // 传id
    recommendWorkDVC.bID = [self.recommendWorksArr[indexPath.row] bId];
    NSLog(@"%@", recommendWorkDVC.bID);
    
    [self.navigationController pushViewController:recommendWorkDVC animated:YES];
    [recommendWorkDVC release];
}

// cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  220 * kHeight / 667;
}
// 解析数据(推荐作品表视图)
- (void)createRecommendWorks {
    /**
     *  加一个小菊花
     */
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    // 显示小菊花
    [hub show:YES];
    // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    // 2.让管理类调用get请求
    [manager GET:@"http://www.hunliji.com/v2/api/app/works?commodity_type=1&per_page=20&first_query_time=2015-06-26T01:42:51Z&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果成功返回数据, 接收数据
        NSDictionary *dic = responseObject;
        NSArray *worksArr = dic[@"works"];
        for (NSDictionary *tempDic in worksArr) {
            recommendWorksModel *model = [[recommendWorksModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.recommendWorksArr addObject:model];
            
            
            [model release];
            /**
             *  数据加载成功之后,隐藏小菊花
             */
            [hub hide:YES];
        }
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败,返回错误信息

        


    }];
}
// 创建滚动视图
- (void)createView {
    
    // 2.轮播图scrollview
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200 * kHeight / 667)];
    // 把scrollview放到headerview上
    [self.headerView addSubview:self.scrollView];
    // 设置偏移量
    self.scrollView.contentOffset = CGPointMake(kWidth, 0);
    // 设置scrollview按页滚动
    self.scrollView.pagingEnabled = YES;
    
    
    
    
    // 3.三个展示图（button）
    // 左按钮
    self.leftShowImage = [UIButton buttonWithType:UIButtonTypeCustom];

    self.leftShowImage.frame = CGRectMake(10 * kWidth / 375, self.scrollView.frame.size.height + 10 * kHeight / 667, kWidth / 3 - 20 * kWidth / 375, 100 * kHeight / 667);
    self.leftShowImage.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // 将button添加到headerView上
    [self.headerView addSubview:self.leftShowImage];
    // 添加点击方法
    [self.leftShowImage addTarget:self action:@selector(showImageDetail:) forControlEvents:UIControlEventTouchUpInside];
    
    // 中间展示图按钮
    self.middleShowImage = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.middleShowImage.frame = CGRectMake(self.scrollView.frame.size.width / 3 + 10 * kWidth / 375, self.scrollView.frame.size.height + 10 * kHeight / 667, self.scrollView.frame.size.width / 3 - 20 * kWidth / 375, 100 * kHeight / 667);
    self.middleShowImage.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // 将button添加到headerView上
    [self.headerView addSubview:self.middleShowImage];
    // 添加点击方法
    [self.middleShowImage addTarget:self action:@selector(showImageDetail:) forControlEvents:UIControlEventTouchUpInside];
    
    // 右边展示图按钮
    self.rightShowImage = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.rightShowImage.frame = CGRectMake(self.scrollView.frame.size.width / 3  * 2+ 10 * kWidth / 375, self.scrollView.frame.size.height + 10 * kHeight / 667, self.scrollView.frame.size.width / 3 - 20 * kWidth / 375, 100 * kHeight / 667);
    self.rightShowImage.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // 将button添加到headerView上
    [self.headerView addSubview:self.rightShowImage];
    // 添加点击方法
    [self.rightShowImage addTarget:self action:@selector(showImageDetail:) forControlEvents:UIControlEventTouchUpInside];
    
    
    // 4.超值折扣（scrollview）
    // 超值折扣按钮
    self.discountButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.discountButton setTitle:@"超值折扣                                              更多>>" forState:UIControlStateNormal];
    self.discountButton.frame = CGRectMake(0, 329 * kHeight / 667, kWidth, 40 * kHeight / 667);
//    self.discountButton.layer.borderWidth = 1;
//    self.discountButton.layer.cornerRadius = 10;
    
    [self.headerView addSubview:self.discountButton];
    self.discountButton.backgroundColor = [UIColor whiteColor];
    // 按钮字体颜色
    self.discountButton.tintColor = [UIColor blackColor];
    // 添加触发方法
    [self.discountButton addTarget:self action:@selector(discountAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    /**
     *  超值折扣集合视图
     */
    // 1.超值折扣集合视图布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 2.设置每个item的大小(cell)
    flowLayout.itemSize = CGSizeMake(kWidth - 20 * kWidth / 375, 215 * kHeight / 667);
    // 3.section中cell的边界范围
    // 取代上面的行间距
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * kWidth / 375, 10 * kHeight / 667, 10 * kWidth / 375, 0 * kHeight / 667);
    // 设置滑动方向(水平滑动)
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    // 4.超值折扣集合视图类
    self.discountCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, (320 + 50) * kHeight / 667, kWidth, 250 * kHeight / 667) collectionViewLayout:flowLayout];
    self.discountCollectionView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // 5.设置代理,设置数据源
    self.discountCollectionView.delegate = self;
    self.discountCollectionView.dataSource = self;
    // 6.把集合视图添加到header上
    [self.headerView addSubview:self.discountCollectionView];
    // 7.内存管理
    [_discountCollectionView release];
    // 8.注册
    // 第一个参数:需要指定注册类型
    // 第二个参数: 重用标志
    [self.discountCollectionView registerClass:[discountDetailCell class] forCellWithReuseIdentifier:@"reuseDiscountDetailCell"];
    
    
    
    // 5.标签推荐(collectionView)
    // 标签推荐上面按钮
    self.labelRecommendButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.labelRecommendButton setTitle:@"标签推荐                                              更多>>" forState:UIControlStateNormal];
//    self.labelRecommendButton.layer.borderWidth = 1;
//    self.labelRecommendButton.layer.cornerRadius = 10;
    
    self.labelRecommendButton.frame = CGRectMake(0, (200 + 100 + 150 + 20 + 10 + 50 + 20 + 30 + 50) * kHeight / 667, kWidth, 30 * kHeight / 667);
    [self.headerView addSubview:self.labelRecommendButton];
    self.labelRecommendButton.backgroundColor = [UIColor whiteColor];
    self.labelRecommendButton.tintColor = [UIColor blackColor];
    //添加触发方法
    [self.labelRecommendButton addTarget:self action:@selector(labelRecommendButtonAction) forControlEvents:UIControlEventTouchUpInside];
    // 标签推荐集合视图布局
    UICollectionViewFlowLayout *floowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 每个item的大小(cell)
    floowLayout.itemSize = CGSizeMake(100 * kWidth / 375, (kWidth - 0 * kWidth / 375) / 3 );
    // 设置最小的行间距
    floowLayout.minimumLineSpacing = 10 * kWidth / 375;
    // 取代上面的行间距设置(section中cell的边界范围)
    floowLayout.sectionInset = UIEdgeInsetsMake(10 * kWidth / 375, 10 * kHeight / 667, 10 * kWidth / 375, 10 * kHeight / 667);
    // 标签推荐集合视图
    self.labelRecommend = [[UICollectionView alloc] initWithFrame:CGRectMake(0, (200 + 100 + 150 + 20 + 10 + 50 + 50 + 30 + 50) * kHeight / 667, kWidth - 10 * kWidth / 375, 310 * kHeight / 667) collectionViewLayout:floowLayout];
    // 协议
    self.labelRecommend.dataSource = self;
    self.labelRecommend.delegate = self;
    // 把集合视图添加到header上
    [self.headerView addSubview:self.labelRecommend];
    // 内存管理
    [_labelRecommend release];
    // 为集合视图设置背景颜色
    self.labelRecommend.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // 注册
    // 第一个参数:需要制定注册类型
    // 第二个参数:重用的标志
    [self.labelRecommend registerClass:[recommendCell class] forCellWithReuseIdentifier:@"reuseCel"];
    
}
// 标签推荐按钮的触发方法
- (void)labelRecommendButtonAction {
    wholeLabelViewController *wholeLabelVC = [[wholeLabelViewController alloc] init];
    [self.navigationController pushViewController:wholeLabelVC animated:YES];
    // 把推荐标签的图片信息传过去
    wholeLabelVC.labelRecommendInfoArr = self.labelRecommendArr;
    [wholeLabelVC release];
}
// 超值折扣按钮触发方法
- (void)discountAction {
    packageViewController *packageVC = [[packageViewController alloc] init];
    [self.navigationController pushViewController:packageVC animated:YES];
    [packageVC release];
}
// 展示图的触发方法
- (void)showImageDetail:(UIButton *)button {
    showImageDetailViewController *showImageDVC = [[showImageDetailViewController alloc] init];
    if (button == self.leftShowImage) {
        // 属性传值
        showImageDVC.webURL = [self.showPicArr[0] target_url];
    } else if (button == self.middleShowImage) {
        // 属性传值
        showImageDVC.webURL = [self.showPicArr[1] target_url];
    } else {
        // 属性传值
        showImageDVC.webURL = [self.showPicArr[2] target_url];
    }
    
    [self.navigationController pushViewController:showImageDVC animated:YES];
    [showImageDVC release];
}
// 标签推荐集合视图的方法
// cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([collectionView isEqual:self.labelRecommend]) {
        return self.labelRecommendArr.count;
    } else {
        return self.discountArr.count;
    }
    
}
// 设置cell(标签推荐)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual:self.labelRecommend]) {
        // 获取数据
        labelRecommendModel *model = self.labelRecommendArr[indexPath.row];
        //
        recommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseCel" forIndexPath:indexPath];
        cell.myImageView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        // 把图片赋给cell
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:model.image_path]];
        // 标签推荐图片标题
        UILabel *recommendTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.myImageView.frame.size.width - 20 * kWidth / 375, 5 * kHeight / 667, 15 * kWidth / 375, cell.myImageView.frame.size.height - 10 * kHeight / 667)];
        recommendTitleLabel.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        // 获取图片标题信息
        recommendTitleLabel.text = [model name];
        // 文字自适应
        recommendTitleLabel.numberOfLines = 0;
        recommendTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        CGSize size = [recommendTitleLabel sizeThatFits:CGSizeMake(recommendTitleLabel.frame.size.width, MAXFLOAT)];
        recommendTitleLabel.frame = CGRectMake(cell.myImageView.frame.size.width - 20 * kWidth / 375, 10 * kHeight / 667, 15 * kWidth / 375, size.height);
        recommendTitleLabel.frame= CGRectMake(recommendTitleLabel.frame.origin.x, recommendTitleLabel.frame.origin.y, recommendTitleLabel.frame.size.width, recommendTitleLabel.frame.size.height);
        // 设置字体
        recommendTitleLabel.font = [UIFont systemFontOfSize:13];
        
        // 把标签加到图片上
        [cell addSubview:recommendTitleLabel];
        // 内存管理
        [recommendTitleLabel release];
        return cell;
    } else {
        discountDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseDiscountDetailCell" forIndexPath:indexPath];
        // 获取数据
        discountModel *model = self.discountArr[indexPath.row];
        cell.myImageView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        // 把图片赋给cell
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_path]];
        // 超值折扣对应信息的label
        // 标题标签
//        cell.titleLabel.backgroundColor = [UIColor whiteColor];
        // 添加数据
        NSString *discountTitleStr = model.title;
        cell.titleLabel.text = [NSString stringWithFormat:@" %@", discountTitleStr];
        // 市场价标签

        cell.market_priceLabel.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        // 添加数据
        NSString *marketPriceStr = [model.market_price stringValue];
        //
        cell.market_priceLabel.text = [NSString stringWithFormat:@"  市场价:%@元", marketPriceStr];
        // 设置字体
        cell.market_priceLabel.font = [UIFont systemFontOfSize:12];
        // 设置字体颜色
        cell.market_priceLabel.textColor = [UIColor grayColor];

        
        // 实际价格标签

        cell.actual_priceLabel.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
        // 添加数据
        NSString *actualPrice = [model.actual_price stringValue];
        cell.actual_priceLabel.text = [NSString stringWithFormat:@"          现价:%@元", actualPrice];
        // 设置字体颜色
        cell.actual_priceLabel.textColor = [UIColor redColor];
        return cell;
    }
    
}
// 被选中的cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual:self.discountCollectionView]) {
        discountModel *model = self.discountArr[indexPath.row];
        discountDetailViewController *discountDVC = [[discountDetailViewController alloc] init];
        discountDVC.ID = (NSString *)model.id;
        [self.navigationController pushViewController:discountDVC animated:YES];
        [discountDVC release];
    } else {
        labelRecommendModel *model = self.labelRecommendArr[indexPath.row];
        labelRecommendDetailViewController *labelRecommendDVC =
        
        [[labelRecommendDetailViewController alloc] init];
        // 传值
        labelRecommendDVC.ID = model.id;
        labelRecommendDVC.index = indexPath.row;

        labelRecommendDVC.detaiTitle = model.name;

        [self.navigationController pushViewController:labelRecommendDVC animated:YES];
        [labelRecommendDVC release];
    }
    
}
// 解析数据(标签推荐集合视图)
- (void)createLabelRecommend {
    // 添加小菊花(加载等候框)
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    // 显示小菊花
    [hub show:YES];
    // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // 2.让管理类调用get请求
    [manager GET:@"http://hunliji.com/v1/api/app/marks/recommend" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果成功返回数据,接收数据
        NSDictionary *resultDic = responseObject;
        NSArray *marks = resultDic[@"marks"];

        for (NSDictionary *tempDic in marks) {
            labelRecommendModel *model = [[labelRecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];

            [self.labelRecommendArr addObject:model];

            [model release];
        }
        // 不要忘了重载
        [self.labelRecommend reloadData];

        // 数据加载完之后隐藏小菊花
        [hub hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败,返回error信息

        

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你的网络有问题哦." delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
        [alert release];

    }];
}
// 定时器执行方法
- (void)changImage {
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + kWidth, 0) animated:YES];
    if (self.scrollView.contentOffset.x == kWidth * (self.picArr.count - 1)) {
        self.scrollView.contentOffset = CGPointMake(kWidth, 0);
    }
}
// 解析数据(轮播图,展示图)
- (void)createData {
    // 在数据加载之前添加小菊花
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    // 显示小菊花
    [hub show:YES];
    // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // 2.让管理类调用get请求
    [manager GET:@"http://hunliji.com/v1/api/app/posters.json?app_version=5.5.0&cid=0&device=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果成功返回数据
        // 接收数据

        NSDictionary *dic = responseObject;
        NSArray *sites = dic[@"sites"];
        //2.给轮播图的数组里添加数据
       // 存储轮播图的数组，数组中是存储轮播图的相关信息的字典
        NSArray *postersArr = sites[0][@"posters"];
        if (postersArr.count > 1) {
            // 先把最后一个图片添加到数组中
            [self.picArr addObject:postersArr[postersArr.count - 1][@"image_path"]];
            
            for (NSDictionary *postDic in postersArr) {
                [self.picArr addObject:postDic[@"image_path"]];
            }
            // 最后把第一个图片添加到数组的最后
            [self.picArr addObject:self.picArr[1]];
            // 给轮播图赋值
            // 设置滚动范围
            self.scrollView.contentSize = CGSizeMake(kWidth * self.picArr.count, 0);
        } else {
            for (NSDictionary *postDic in postersArr) {
                [self.picArr addObject:postDic[@"image_path"]];
            }
            // 设置滚动范围
            self.scrollView.contentSize = CGSizeMake(kWidth * self.picArr.count, 0);
        }
        
        // 把图片加到scrollview上
        for (NSInteger i = 0; i < self.picArr.count; i++) {
            UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * i, 0, kWidth, self.scrollView.frame.size.height )];
            [self.scrollView addSubview:imageview];
            [imageview sd_setImageWithURL:[NSURL URLWithString:self.picArr[i]]];
            
            [imageview release];
        }
        
        
        // 3.给三个展示图（按钮）数组中添加数据
        // 存数展示图的数组，存数的是包含展示图相关信息的字典
        NSArray *showArr = sites[1][@"posters"];
        for (NSDictionary *tempDic in showArr) {
            showImageModel *model = [[showImageModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.showPicArr addObject:model];
            [model release];
        }
        // 传值
        [self.leftShowImage setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.showPicArr[0] image_path]]]] forState:UIControlStateNormal];
        [self.middleShowImage setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.showPicArr[1] image_path]]]] forState:UIControlStateNormal];
        [self.rightShowImage setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.showPicArr[2] image_path]]]] forState:UIControlStateNormal];
        // 数据加载成功之后,隐藏小菊花
        [hub hide:YES];
        
        // 加一个定时器
        if (self.picArr.count > 1) {
            [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changImage) userInfo:nil repeats:YES];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败返回error信息
    }];
}
// 解析数据(超值折扣)
- (void)createDiscountData {
    // 在数据加载之前添加一个等待提示框
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    // 显示等待提示框
    [hub show:YES];
    // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // 2.让管理类调用get请求
    [manager GET:@"http://www.hunliji.com/v1/api/app/works/discount?cid=0" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果成功返回数据
        // 接收数据
        NSDictionary *resultDic = responseObject;

        NSArray *worksArr = resultDic[@"works"];
        for (NSDictionary *tempDic in worksArr) {
            discountModel *model = [[discountModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            
            
     
            [self.discountArr addObject:model];

            [model release];
        }
        
       
        // 数据加载成功之后,隐藏小菊花
        [self.discountCollectionView reloadData];
        
        [hub hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败返回error信息

        


    }];
}

// 解析超值折扣详情页面(案例)的信息
- (void)createDiscountDetail:(NSString *)urlStr {
   // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    // 2.让管理类调用get请求
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
       // 如果成功返回数据, 接收数据
        NSDictionary *resultDic = responseObject;
        discountDetailModel *model = [[discountDetailModel alloc] init];
        [model setValuesForKeysWithDictionary:resultDic];
        [self.discountDetailArr addObject:model];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败,返回错误信息
    }];
}
// 超值折扣图片点击手势触发的方法
- (void)discountImageTapAction:(UITapGestureRecognizer *)tap :(discountDetailViewController *)discountDVC{
    // 跳转的页面
    discountDVC = [[discountDetailViewController alloc] init];
    discountDVC.title = @"1";
    [self.navigationController pushViewController:discountDVC animated:YES];
    [discountDVC release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
